//
//  UIButton+SGExtension.swift
//  HomeFurnishing
//
//  Created by  on 2019/1/7.
//  Copyright © 2019 蜀归科技. All rights reserved.
//

import Foundation
import UIKit

//extension UIButton : ZMCompatible{}

extension ZM where Base : UIButton {
    /// 倒计时
    public func countDown(count: Int, backBlock: ((_ endCount: Bool)->())? = nil){
        if let block = backBlock {
            block(false)
        }
        base.isUserInteractionEnabled = false
        base.setTitleColor(UIColor.zm.color999999(), for: .normal)
        base.layer.borderColor = UIColor.zm.color999999().cgColor
        var remainingCount: Int = count {
            willSet {
                base.setTitle("\(newValue)S", for: .normal)
                if newValue <= 0 {
                    if let block = backBlock {
                        block(true)
                    }
                    base.isUserInteractionEnabled = true
                    base.setTitle("获取验证码", for: .normal)
                    base.setTitleColor(defaut_color, for: .normal)
                    base.layer.borderColor = defaut_color.cgColor
                }
            }
        }
        
        let codeTimer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .seconds(1))
        codeTimer.setEventHandler(handler: {
            DispatchQueue.main.async {
                remainingCount -= 1
                if remainingCount <= 0 {
                    codeTimer.cancel()
                }
            }
        })
        
        codeTimer.resume()
    }
    
    /// 添加纯色背景角标
    public func addSuperCript(bgColor: UIColor = .red) {
        let lab = UILabel()
        lab.layer.cornerRadius = 4
        lab.layer.masksToBounds = true
        lab.backgroundColor = bgColor
        base.addSubview(lab)
        lab.snp.makeConstraints { (make) in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(8)
        }
    }
    
    /// 添加数字角标
    public func addNumberSuperCript(bgColor: UIColor, num: String) {
        for view in base.subviews {
            if view.isKind(of: UILabel.self){
                view.removeFromSuperview()
            }
        }
        if num == "0" {
            return
        }
        let numStr = String(format: "%@", Int(num)! > 99 ? "99+" : num)
        let lab = ZMLabel()
        lab.layer.cornerRadius = 8
        lab.layer.masksToBounds = true
        lab.text = numStr
        lab.textAlignment = .center
        lab.font = UIFont.zm.fontMedium(size: 10)
        lab.textColor = UIColor.white
        lab.backgroundColor = bgColor
        lab.zm_edgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        base.addSubview(lab)
        lab.snp.makeConstraints { (make) in
            make.top.equalTo(-9)
            make.right.equalTo(4)
            make.width.greaterThanOrEqualTo(16)
            make.height.equalTo(16)
        }
    }
    
    /// 更新角标数字
    func updateNumberSuperCript(num: Int) {
        for view in base.subviews {
            if view.isKind(of: UILabel.self){
                (view as! UILabel).text = "\(num)"
            }
        }
    }
    
    /// 清除角标
    public func clearSuperCript() {
        for view in base.subviews {
            if view.isKind(of: UILabel.self){
                view.removeFromSuperview()
            }
        }
    }
    
    // kf 设置图片
    func setImage(urlStr : String , state: UIControl.State, placeholder : String? = "def_img",options : KingfisherOptionsInfo? = nil) {
        let resource = ImageResource(downloadURL: URL.init(string: urlStr) ?? URL(fileURLWithPath: placeholder ?? ""), cacheKey: urlStr)
        base.kf.setImage(with: resource, for: state, placeholder: UIImage(named: placeholder ?? "def_img"))
    }
    
}
